<template>
  <div class="min-h content-layout">
    <div class="bg-wt">
      <SwitchBar ref="tabBar" :data="tabData" :floorIndex="floorIndex" @handle-floor-edit="handleFloorEdit"
                 @handle-add="handleFloorAdd" @change-id="getFloorId"/>
      <FloorInfo @handle-add="handleRoomAdd"/>
      <div>
        <HouseList :floor-data="tabData" :room-voList="roomVoList" @handle-edit="handleRoomEdit"
                   @handle-delete="handleDelete" @handle-add="handleBedAdd" @handle-bed-edit="handleBedEdit"
                   @handle-check="handleRoomCheck"/>
      </div>
      <AddFloor ref="floor" :data="floorData" :is-room-delete="isRoomDelete" :max-number="maxNum" :min-number="minNum"
                :room-voList="roomVoList" :title="textVal" :visible="floorVisible"
                @handle-delete="handleFloorDelete"
                @handle-close="handleFloorClose" @handle-add="handleFloorAddSubmit"
                @handle-edit="handleFloorEditSubmit"/>
      <AddRoom ref="room" :data="roomData" :max-number="maxNum" :min-number="minNum" :room-type-data="roomTypeData"
               :title="textVal" :visible="roomVisible" @handle-close="handleRoomClose"
               @handle-add="handleRoomAddSubmit" @handle-edit="handleRoomEditSubmit"/>
      <AddBed ref="bed" :data="bedData" :max-number="maxNum" :min-number="minNum" :title="textVal" :visible="bedVisible"
              @handle-close="handleBedClose" @handle-add="handleBedAddSubmit"
              @handle-edit="handleBedEditSubmit"/>
      <Delete :delete-text="operateText" :visible="deleteVisible" @handle-close="handleDeleteClose"
              @handle-delete="handleDeleteSubmit"/>
    </div>
  </div>
</template>

<script name="Floor" setup>
const {proxy} = getCurrentInstance();
import useUserStore from '@/store/modules/user';
import {getRoomTypesList} from '@/api/nursing/roomType';
import {
  addBed,
  addFloor,
  addRoom,
  delBed,
  delFloor,
  delRoom,
  getBedDetails,
  getFloorDetails,
  getRoomBedList,
  getRoomDetails,
  listFloor,
  updateBed,
  updateFloor,
  updateRoom,
} from '@/api/nursing/floor';
import SwitchBar from './components/SwitchBartop.vue';
import HouseList from './components/List.vue';
import FloorInfo from './components/FloorInfo.vue';
import AddFloor from './components/AddFloor.vue';
import AddRoom from './components/AddRoom.vue';
import AddBed from './components/AddBed.vue';
import Delete from '@/components/DelectDialog/index.vue'

const userStore = useUserStore();
const router = useRouter();
const route = useRoute();
const textVal = ref('');

const tabBar = ref(null);
const tabData = ref([]);
const floorData = ref({});
const floorId = ref('');
const floorVisible = ref(false);
const minNum = ref(null);
const maxNum = ref(99999999);
const isOperateSuccess = ref(false);
const floorIndex = ref(null);
const isRoomDelete = ref(false);
const deleteFloorId = ref('');
const floor = ref(null);
const roomVoList = ref([]);
const roomId = ref('');
const roomVisible = ref(false);
const roomData = ref({});
const roomTypeData = ref([]);
const room = ref(null);
const bed = ref(null);
const bedId = ref('');
const bedVisible = ref(false);
const bedData = ref({});
const bedSelectData = ref({});
const tabCheck = ref(false);
const deleteVisible = ref(false);
const operateText = ref('');
const delectType = ref(null);

onMounted(() => {
  getAllFloorList();
  getHouseType();
});

// 获取楼层数据
const getAllFloorList = async () => {
  const res = await listFloor();
  if (res.code === 200) {
    tabData.value = res.data;
    const floorBase = userStore.floorInfo;
    if (tabData.value.length > 0) {
      tabData.value.forEach((val, index) => {
        if (floorBase.code) {
          if (floorBase.code > val.code) {
            tabBar.value.currentId = index + 1;
            floorId.value = tabData.value[index + 1].id;
          } else if (
              (floorBase.code === val.code && floorBase.name === val.name) ||
              floorBase.id === val.id
          ) {
            tabBar.value.currentId = index;
            floorId.value = tabData.value[index].id;
          }
        } else {
          if (index === floorIndex.value) {
            floorId.value = tabData.value[floorIndex.value].id;
          }
          if (isOperateSuccess.value && floorId.value !== val.id) {
            tabBar.value.currentId = floorIndex.value;
            if (tabData.value.length === floorIndex.value) {
              tabBar.value.currentId = floorIndex.value - 1;
              floorId.value = tabData.value[floorIndex.value - 1].id;
            }
            if (index === floorIndex.value) {
              floorId.value = tabData.value[floorIndex.value].id;
            }
            isOperateSuccess.value = false;
          }
        }
      });
    }
    if (floorId.value === '') {
      floorId.value = tabData.value[0].id;
    }
    getRoomListData();
  }
};

// 编辑楼层、获取楼层id
const handleFloorEdit = (id, i) => {
  floorIndex.value = i;
  floorId.value = id;
  floorVisible.value = true;
  handleFloorData(floorId.value);
  userStore.setFloorInfo({});
  textEdit();
};

// 新增楼层弹层
const handleFloorAdd = () => {
  floorData.value = {
    code: 1,
  };
  textAdd();
  floorVisible.value = true;
};

// 关闭楼层弹层
const handleFloorClose = () => {
  floorVisible.value = false;
};

// 楼层点击删除时显示删除弹层
const handleFloorDelete = (id) => {
  floorId.value = '';
  deleteFloorId.value = id;
  handleDelete();
};

// 获取楼层id
const getFloorId = (id, i, isCheckTab) => {
  floorIndex.value = i;
  floorId.value = id;
  getAllFloorList();
  userStore.setFloorInfo({});
};

// 获取楼层详情
const handleFloorData = async (id) => {
  const res = await getFloorDetails(id);
  if (res.code === 200) {
    floorData.value = res.data;
  }
};

// 新增楼层提交
const handleFloorAddSubmit = async (val) => {
  const res = await addFloor(val);
  if (res.code === 200) {
    getAllFloorList();
    handleFloorClose();
    floor.value.handleClear();
    proxy.$modal.msgSuccess('新增成功');
  }
};

// 编辑楼层提交
const handleFloorEditSubmit = async (val) => {
  const params = {
    id: floorData.value.id,
    code: val.code,
    name: val.name,
  };
  const res = await updateFloor(params);
  if (res.code === 200) {
    getAllFloorList();
    handleFloorClose();
    floor.value.handleClear();
    proxy.$modal.msgSuccess('编辑成功');
  }
};

// 删除楼层
const handleFloorDeleteSubmit = async () => {
  isOperateSuccess.value = false;
  const res = await delFloor(deleteFloorId.value);
  if (res.code === 200) {
    userStore.setFloorInfo({});
    isOperateSuccess.value = true;
    floor.value.handleClear();
    getAllFloorList(); // 刷新列表
    floorVisible.value = false; // 隐藏楼层弹层
    handleDeleteClose(); // 关闭删除弹层
  }
};

// 根据不同的楼层id获取房间数据
const getRoomListData = async () => {
  const res = await getRoomBedList(floorId.value);
  if (res.code === 200) {
    roomVoList.value = res.data;
  }
};

// 获取房间类型
const getHouseType = async () => {
  const res = await getRoomTypesList();
  if (res.code === 200) {
    roomTypeData.value = res.data.filter((val) => val.status === 1);
  }
};

// 新增房间提交
const handleRoomAddSubmit = async (val) => {
  const params = {
    ...val,
    floorId: floorId.value,
  };
  const res = await addRoom(params);
  if (res.code === 200) {
    getRoomListData();
    handleRoomClose();
    room.value.handleClear();
    proxy.$modal.msgSuccess('新增成功');
  }
};

// 获取房间详情
const handleRoomData = async (id) => {
  const res = await getRoomDetails(id);
  if (res.code === 200) {
    roomData.value = res.data;
  }
};

// 编辑房间提交
const handleRoomEditSubmit = async (val) => {
  const res = await updateRoom(val);
  if (res.code === 200) {
    getRoomListData();
    handleRoomClose();
    room.value.handleClear();
    proxy.$modal.msgSuccess('编辑成功');
  }
};

// 删除房间提交
const handleRoomDeleteSubmit = async () => {
  const res = await delRoom(roomId.value);
  if (res.code === 200) {
    getRoomListData();
    handleDeleteClose();
  }
};

// 新增房间
const handleRoomAdd = () => {
  textAdd();
  roomVisible.value = true;
};

// 编辑房间
const handleRoomEdit = (id) => {
  textEdit();
  roomId.value = id;
  handleRoomData(id);
  roomVisible.value = true;
};

// 查看房间
const handleRoomCheck = (id) => {
  handleRoomData(id);
  checkVisible.value = true;
};

// 关闭查看房间
const handleCheckRoomClose = () => {
  room.value.handleClear();
  checkVisible.value = false;
};

// 关闭房间弹层
const handleRoomClose = () => {
  roomVisible.value = false;
};

// 新增床位提交
const handleBedAddSubmit = async (val) => {
  const params = {
    ...val,
    roomId: roomId.value,
  };
  const res = await addBed(params);
  if (res.code === 200) {
    getRoomListData();
    handleBedClose();
    bed.value.handleClear();
    proxy.$modal.msgSuccess('新增成功');
  }
};

// 编辑床位提交
const handleBedEditSubmit = async (val) => {
  const params = {
    ...val,
    roomId: roomId.value,
  };
  const res = await updateBed(params);
  if (res.code === 200) {
    getRoomListData();
    handleBedClose();
    bed.value.handleClear();
    proxy.$modal.msgSuccess('编辑成功');
  }
};

// 获取床位详情
const handleBedData = async (id) => {
  const res = await getBedDetails(id);
  if (res.code === 200) {
    bedData.value = res.data;
  }
};

// 删除床位提交
const handleBedDeleteSubmit = async () => {
  const res = await delBed(bedId.value);
  if (res.code === 200) {
    getRoomListData();
    handleDeleteClose();
  }
};

// 床位新增
const handleBedAdd = (id) => {
  roomId.value = id;
  textAdd();
  bedVisible.value = true;
};

// 床位编辑
const handleBedEdit = (id) => {
  textEdit();
  bedId.value = id;
  handleBedData(id);
  bedVisible.value = true;
};

// 床位关闭
const handleBedClose = () => {
  bedVisible.value = false;
};

// 新增文本
const textAdd = () => {
  textVal.value = '新增';
};

// 编辑文本
const textEdit = () => {
  textVal.value = '编辑';
};

// 显示删除弹层
const handleDelete = (type, val) => {
  delectType.value = type;
  if (type === 2) {
    roomId.value = val.id;
    operateText.value = '房间';
  } else if (type === 3) {
    bedId.value = val.id;
    operateText.value = '床位';
  } else {
    operateText.value = '楼层';
  }
  deleteVisible.value = true;
};

// 关闭删除弹层
const handleDeleteClose = () => {
  deleteVisible.value = false;
};

// 删除提交
const handleDeleteSubmit = () => {
  const type = delectType.value
  if (type === 2) {
    handleRoomDeleteSubmit()
  } else if (type === 3) {
    handleBedDeleteSubmit()
  } else {
    handleFloorDeleteSubmit()
  }
}
</script>
<style lang="less" src="./index.scss"></style>
